package com.lmc.forum.services.impl;

import com.lmc.forum.common.AppResult;
import com.lmc.forum.common.ResultCode;
import com.lmc.forum.dao.BoardMapper;
import com.lmc.forum.exception.ApplicationException;
import com.lmc.forum.model.Board;
import com.lmc.forum.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {


    @Autowired
    BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        if(num <= 0){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE.getMessage()));

        }
        return boardMapper.selectByNum(num);
    }
    @Override
    public void addOneArticleCountById(Long id){
        if (id == null || id <= 0){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT.getMessage()));
        }

        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
// 1. 找不到该板块
            log.warn(ResultCode.ERROR_IS_NULL.toString() + ", board id = " + id);
// 2. 抛出异常
            throw new
                    ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }

        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if (row != 1) {
            log.warn(ResultCode.FAILED.toString() + ", 受影响的⾏数不等于 1 .");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

    }

    @Override
    public Board selectById(Long id) {
        return boardMapper.selectByPrimaryKey( id);
    }


    @Override
    public void subOneArticleCountById(Long id) {
// ⾮空校验
        if (id == null || id <= 0) {
// 打印⽇志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
// 抛出异常
            throw new
                    ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
// 查询版块详情
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
// 打印⽇志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString() + ", board id = " + id);
// 抛出异常
            throw new
                    ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
// 构造更新对象
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);
// 判断减1之后是否⼩于0
        if (updateBoard.getArticleCount() < 0) {
// 如果⼩于0那么设置为0
            updateBoard.setArticleCount(0);
        }
// 调⽤DAO
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if (row != 1) {
            log.warn(ResultCode.FAILED.toString() + ", 受影响的⾏数不等于 1 .");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }


}
